(function () {
	angular
	.module('ydApp.doc')
	.config(function ($stateProvider) {
		$stateProvider.state('doc-good', {
			url: '/doc-good/:docId',
			controller: "docGoodCtrl",
			templateUrl: 'views/docs/doc-good.html'
		});
		$stateProvider.state('doc-good-succ', {
			url: '/doc-good-succ',
			templateUrl: 'views/docs/doc-good-succ.html'
		});
	})
	.controller("docGoodCtrl", docGoodCtrl);

	function docGoodCtrl ($scope, xhr, $stateParams, popup, localStore, $ionicLoading) {
		var vm = $scope.vm = {};
		var user = localStore.get("user");
		xhr.get("HealthInquiry/com/doctor/findById", {
			id: $stateParams.docId
		}).then(function (res) {
			vm.doc = res.data;
		});
		vm.costMoney = 10;
		vm.selectCost = function (moeny) {
			vm.costMoney = moeny;
		}
		vm.save = function (){
			// if (!vm.content) {
			// 	popup.alert('请选择图片');
			// 	return;
			// }
			$ionicLoading.show({
				template: '正在提交…'
			});
			xhr.get("HealthInquiry/com/doctorPraise/saveOrUpdate", {
				userId: user.id,
				doctorId: $stateParams.docId,
				praiseCost: vm.costMoney,
				content: vm.content
			}).then(function (res) {
				$ionicLoading.hide();
				popup.alert('提交成功');
				vm.data = [];
				vm.condition.pageIndex = 0;
				vm.load();
			});
		};

		vm.data = [];
		vm.nodata = false;
		vm.condition = {
			pageIndex: -1,
			sortField: 'modifyTime',
			sortOrder: 'desc',
			doctorId: $stateParams.docId
		}
		vm.loadMore = function() {
			vm.condition.pageIndex++;
			vm.load().then(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			});
		}
		vm.load = function() {
			return xhr.get("HealthInquiry/com/doctorPraise/findByDoctorId", vm.condition).then(function(result) {
				if (result.data.length == 0) {
					vm.nodata = true;
				}
				vm.data = vm.data.concat(result.data);
			})
		}
	}
})();